import { createDiscreteApi } from 'naive-ui'

/**
 * 用法：
 * useMessage().success("xxx")
 * useMessage().info("xxx")
 * useMessage().warning("xxx")
 * useMessage().loading("xxx")
 * useMessage().error("xxx")
 * @returns 
 */


var ksd_umessage = null
export const useMessage = () => {
  if (!ksd_umessage) {
    const {message} = createDiscreteApi(['message'])
    ksd_umessage = message
  }
  //是否关闭当前实例其他的所有的message
  ksd_umessage.destroyAll()
  return ksd_umessage
}

/**
 * 用法：
 * https://www.naiveui.com/zh-CN/os-theme/components/dialog
 * useDialog().[success/warning/error/]({
    title: '警告',
    content: '你确定？',
    positiveText: '确定',
    negativeText: '不确定',
    onPositiveClick: () => {
    message.success('确定')
    },
    onNegativeClick: () => {
    message.error('不确定')
    }
})
 * @returns 
 */
var ksd_udialog = null
export const useDialog = () => {
  if (!ksd_udialog) {
    const {dialog} = createDiscreteApi(['dialog'])
    ksd_udialog = dialog
  }
  return ksd_udialog
}

/**
 * 用法：
 * useNotification().info("xxx")
 * useNotification().success("xxx")
 * useNotification().warning("xxx")
 * useNotification().error("xxx")
 * @returns 
 */
var ksd_unotification = null
export const useNotification = () => {
  if (!ksd_unotification) {
    const {notification} = createDiscreteApi(['notification'])
    ksd_unotification = notification
  }

  return {
    main(type, title, content, duration) {
      // 清除所有之前的通知
      //ksd_unotification.destroyAll()
      ksd_unotification[type]({
        content: title,
        meta: content,
        duration: duration || 3000,
        keepAliveOnHover: true
      })
    },
    success(title, content, duration = 3000) {
      return this.main('success', title, content, duration)
    },
    error(title, content, duration = 3000) {
      return this.main('error', title, content, duration)
    },
    warning(title, content, duration = 3000) {
      return this.main('warning', title, content, duration)
    },
    info(title, content, duration = 3000) {
      return this.main('info', title, content, duration)
    }
  }
}

/**
 * 用法：
 * useLoading().start()
 * useLoading().finish()
 * @returns 
 */
var ksd_uloadingBar = null
export const useLoading = () => {
  if (!ksd_uloadingBar) {
    const {loadingBar} = createDiscreteApi(['loadingBar'])
    ksd_uloadingBar = loadingBar
  }
  return ksd_uloadingBar
}
